<?php

namespace application\Module;

use application\Comm\Func;
use library\Core\Config;
use library\Core\Db\CDbCriteria;
use application\Model\ZxadimgModel;
use library\Core\Singleton;

class Zxadimg extends Singleton
{
    public function getAdList($position_id='')
    {
        $criteria = new CDbCriteria();
        $criteria->select(['id', 'name', 'url', 'src' ,'description','like']);
        if ($position_id !=''){
        	$criteria->addEqCondition('position_id', $position_id);
        }
        $criteria->addEqCondition('status', 1);
        $criteria->addEqCondition('is_del', 0);
        $criteria->setLimit(5);
        $criteria->addOrderDesc('orderby');
        $model = new ZxadimgModel();
        $result = [];
        $data = $model->getListForArray($criteria);
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $result[$k]['src'] = Func::imgUrl($v['src']);
                $result[$k]['name'] = $v['name'];
                $result[$k]['id'] = $v['id'];
                $result[$k]['url'] = $v['url'];
                $result[$k]['like'] = $v['like'];
                $result[$k]['description'] = $v['description'];
            }
        }
        return $result;
    }

    public function getAd()
    {
        $position = Config::getParamsVal('ZX_AD_IMG_POSITION_VALUE');
        $ad = [];
        foreach ($position as $k => $v) {
            $ad[$k] = $this->getAdList($k);
        }
        return $ad;
    }

    /**
     * @param $ad_id
     * @return int
     * 喜欢
     */
    public function Like($ad_id)
    {
        $criteria = new CDbCriteria();
        $criteria->select(['like']);
        $criteria->addEqCondition('id', $ad_id);
        $criteria->addEqCondition('status', 1);
        $criteria->addEqCondition('is_del', 0);
        $model = new ZxadimgModel();
        $like = $model->getOne($criteria);
        $like++;

        $criteria_update = new CDbCriteria();
        $criteria_update->update(['like'=>$like]);
        $criteria_update->addEqCondition('id', $ad_id);
        return $model->modify($criteria_update);
    }


}
